﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support.Enums;
using MS.InformationManagement.DAL.DTOs;
using office = Microsoft.Office.DocumentManagement.DocumentSets;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Core.Support;

namespace MS.InformationManagement.BLL.ShippingControlTemplates
{
    public class UpdateStatusRadicadoCommand : IBaseCommand
    {
        #region Fields

        IEnumerable<PlanillasDTO> _foundItems;

        string _currentUser;

        #endregion

        #region Constructors

        public UpdateStatusRadicadoCommand( IEnumerable<PlanillasDTO> foundItems )
        {
            _foundItems = foundItems;
        }

        #endregion

        #region Implemented Methods

        public void Execute()
        {
            _currentUser = SPContext.Current.Web.CurrentUser.LoginName;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        currentWeb.AllowUnsafeUpdates = true;
                        currentSite.AllowUnsafeUpdates = true;

                        foreach (var entity in _foundItems)
                        {
                            SPList documentsList = currentWeb.Lists[entity.IdList];
                            SPListItem documentListItem = documentsList.GetItemById(entity.IdItem);
                            SPFolder currentDocItem = documentListItem.Folder != null ? documentListItem.Folder : documentListItem.File.ParentFolder;
                            office.DocumentSet currentDocument = office.DocumentSet.GetDocumentSet(currentDocItem);
                            //_documentsBackup.Add(currentDocument);
                            string documentDepartment = PeopleHelper.GetPropertyUserProfile(GlobalConstants.UP_DEPARTMENT, currentDocument.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_ASSIGNED].ToString());
                            //DeliveryTraceabilityEntry deliveryLogEntry = this.getDeliverEntity(entity, _deliveryForm.Id.ToString(), documentDepartment, documentListItem);
                            currentDocument.Folder.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS_DELIVERED] = entity.Recibido;
                            //currentDocument.UpdateDeliveryLog(deliveryLogEntry);
                            //currentDocument.Log(string.Empty, DateTime.Now, TraceableOperation.Enviado, _currentUser, string.Empty);
                            //currentDocument.Folder.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS] = TermStoreHelper.GetTaxonomyFieldByTerm(GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_STATUS, GlobalConstants.METADATA_TERM_STATUS_SENT);
                            currentDocument.Folder.Item.Update();
                            currentDocument.Folder.Update();
                        }

                        currentSite.AllowUnsafeUpdates = true;
                        currentWeb.AllowUnsafeUpdates = true;
                    }
                }
            });     
        }

        public void Undo()
        {
            //Transaction undo
        }

        #endregion

        #region Private Methods

        private DeliveryTraceabilityEntry getDeliverEntity(EnvíoDeCorrespondencia currentEntity, string deliveryFormId, string department, SPListItem listItem)
        {
            DeliveryTraceabilityEntry logEntry = new DeliveryTraceabilityEntry();
            logEntry.Address = currentEntity.Copia.HasValue && currentEntity.Copia.Value ? listItem[CustomSiteColumns.FLD_COPY_CONTACT_ADDRESS].ToString() : currentEntity.Dirección;
            logEntry.Area = department;
            logEntry.City = currentEntity.City.Label;
            logEntry.DateTime = DateTime.Now;
            //logEntry.Department = department;
            logEntry.FormNumber = deliveryFormId;
            logEntry.To = currentEntity.Copia.HasValue && currentEntity.Copia.Value ? listItem[CustomSiteColumns.FLD_COPY_NAME].ToString() : currentEntity.RemitenteDestinatario;
            logEntry.Operation = DeliveryOperations.Enviado.ToString();
            logEntry.User = _currentUser;
            return logEntry;
        }

        #endregion
    }
}
